package com.it2297.view;

import javax.swing.ImageIcon;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.it2297.component.ImageChooser;
import com.it2297.controller.ProductController;
import com.it2297.model.Product;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Label;
import java.awt.Toolkit;

import javax.swing.JScrollBar;
import javax.swing.border.EmptyBorder;

public class CreateProductView extends JDialog {
	private final ProductController pc = new ProductController(this);
	private final JPanel contentPanel = new JPanel();
	private final Product product = new Product();
	private final JLabel lblprodName = new JLabel("Product Name:");
	private final JLabel lblprodDesc = new JLabel("Description:");
	private final JLabel lblprodPrice = new JLabel("Price:");
	private final JLabel lblprodQuantity = new JLabel("Quantity:");
	private final JTextField ProductNameTxt = new JTextField();
	private final JTextArea ProductDescTxt = new JTextArea();
	private final JTextField ProductPriceTxt = new JTextField();
	private final JTextField ProductQuantityTxt = new JTextField();
	private final JButton btnSubmit = new JButton("Submit");
	private final JComboBox comboBox = new JComboBox();
	private final JLabel lblType = new JLabel("Product Type:");
	private final ImageChooser ic = new ImageChooser();
	private final JButton btnNewButton = new JButton("Choose an Image");
	private final JLabel lbl_img = new JLabel("");

	/**
	 * Create the panel.
	 */
	public CreateProductView() {
		this.setLocationRelativeTo(null);
		setBounds(100, 100, 800, 500);
		getContentPane().setLayout(null);
		contentPanel.setBounds(0, 0, 0, 0);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel);
		contentPanel.setLayout(null);
		ProductQuantityTxt.setBounds(113, 173, 86, 20);
		ProductQuantityTxt.setColumns(10);
		ProductPriceTxt.setBounds(113, 148, 86, 20);
		ProductPriceTxt.setColumns(10);
		ProductNameTxt.setBounds(113, 8, 269, 20);
		ProductNameTxt.setColumns(10);
		for (int i = 0; i < product.getType().size(); i++) {
			comboBox.addItem(product.getType().get(i));
		}
		initGUI();
	}

	public String getProductName() {
		return ProductNameTxt.getText();
	}

	public String getProductDesc() {
		return ProductDescTxt.getText();
	}

	public double getProductPrice() {
		return Double.parseDouble(ProductPriceTxt.getText());
	}

	public int getProductQuantity() {
		return Integer.parseInt(ProductQuantityTxt.getText());
	}

	public String getCategory() {
		return (String) comboBox.getSelectedItem();
	}

	private void initGUI() {
		getContentPane().setLayout(null);
		lblprodName.setBounds(10, 11, 99, 14);

		getContentPane().add(lblprodName);
		lblprodDesc.setBounds(10, 44, 82, 14);

		getContentPane().add(lblprodDesc);
		lblprodPrice.setBounds(10, 151, 67, 14);

		getContentPane().add(lblprodPrice);
		lblprodQuantity.setBounds(10, 176, 82, 14);

		getContentPane().add(lblprodQuantity);

		getContentPane().add(ProductNameTxt);
		ProductDescTxt.setBounds(113, 36, 269, 104);

		getContentPane().add(ProductDescTxt);

		getContentPane().add(ProductPriceTxt);

		getContentPane().add(ProductQuantityTxt);

		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				boolean valid = pc.saveImages();
				if(ProductPriceTxt.getText().isEmpty() || ProductNameTxt.getText().isEmpty() || ProductDescTxt.getText().isEmpty() || ProductQuantityTxt.getText().isEmpty() || valid == false){
					JOptionPane.showMessageDialog(null, "Please fill in all required field");
				}else{
				boolean successful = createProduct();
				if(successful){
				close();
				}else{
					JOptionPane.showMessageDialog(null, "Please choose other Product Name");
				}
				}
			}
		});
		btnSubmit.setBounds(102, 266, 89, 23);

		getContentPane().add(btnSubmit);
		comboBox.setBounds(113, 204, 88, 20);

		getContentPane().add(comboBox);
		lblType.setBounds(10, 207, 99, 14);

		getContentPane().add(lblType);

		btnNewButton.setBounds(213, 266, 163, 23);

		getContentPane().add(btnNewButton);
		lbl_img.setBounds(213, 151, 158, 104);

		getContentPane().add(lbl_img);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ImageIcon img = pc.showImageChooser();
				lbl_img.setIcon(img);
			}
		});
	}

	private boolean createProduct() {
		return  pc.CreateProduct();
	}

	public JPanel getContentPanel() {
		return contentPanel;
	}
	
	public void close(){
		NewShopView eshop = new NewShopView();
		eshop.refresh();
		this.dispose();
	}
	
	


}
